import { Injectable, signal } from '@angular/core';

export interface DragState {
  isDragging: boolean;
  draggedItem: {
    id: string;
    type: 'table' | 'note' | 'area';
    startX: number;
    startY: number;
    offsetX: number;
    offsetY: number;
  } | null;
}

@Injectable({
  providedIn: 'root'
})
export class DragService {
  private readonly _dragState = signal<DragState>({
    isDragging: false,
    draggedItem: null
  });

  readonly dragState = this._dragState.asReadonly();

  startDrag(id: string, type: 'table' | 'note' | 'area', startX: number, startY: number, offsetX: number, offsetY: number) {
    this._dragState.set({
      isDragging: true,
      draggedItem: { id, type, startX, startY, offsetX, offsetY }
    });
  }

  updateDrag(currentX: number, currentY: number) {
    const current = this._dragState();
    if (current.isDragging && current.draggedItem) {
      // 返回新的位置
      return {
        x: currentX - current.draggedItem.offsetX,
        y: currentY - current.draggedItem.offsetY
      };
    }
    return null;
  }

  endDrag() {
    this._dragState.set({
      isDragging: false,
      draggedItem: null
    });
  }
}
